package com.ridanlabs.onelist.android.sync;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ridanlabs.onelist.rpclib.OneListProtocol;

/**
 * Abstract class that will be used as a basis for classes used to represent the JSON rpc methods.
 * @author Nadir Muzaffar
 *
 */
public abstract class JSONRPCMethod {
	
	public static class Sync extends JSONRPCMethod {
		public Sync(String userName) throws JSONException {
			super(OneListProtocol.Sync.METHOD);
			
			this.methodParams.put("user", userName);
		}
		
		public void execute() {
			this.respJSON = RPCClient.execute( this );
		}
		
		JSONObject respJSON;
	}
	
	//ADD LIST ITEM
	public static class AddListItem extends JSONRPCMethod {
		public AddListItem(String itemName, String listName, String userName, int quantity) throws JSONException {
			super(OneListProtocol.AddListItem.METHOD);
			
			this.methodParams.put(OneListProtocol.AddListItem.PARAMETERS[0], itemName);
			this.methodParams.put(OneListProtocol.AddListItem.PARAMETERS[1], listName);
			this.methodParams.put(OneListProtocol.AddListItem.PARAMETERS[2], userName);
			this.methodParams.put(OneListProtocol.AddListItem.PARAMETERS[3], quantity);
		}
	}
	
	//ADD USER TO LIST
	public static class AddUserToList extends JSONRPCMethod {
		public AddUserToList(String userName, String listName) throws JSONException {
			super(OneListProtocol.AddUserToList.METHOD);
			
			this.methodParams.put(OneListProtocol.AddUserToList.PARAMETERS[0], userName);
			this.methodParams.put(OneListProtocol.AddUserToList.PARAMETERS[1], listName);
		}
	}
	
	//CREAT LIST
	public static class CreateList extends JSONRPCMethod {
		public CreateList(String listName, String listCreator) throws JSONException {
			super(OneListProtocol.CreateList.METHOD);
			
			this.methodParams.put(OneListProtocol.CreateList.PARAMETERS[0], listName);
			this.methodParams.put(OneListProtocol.CreateList.PARAMETERS[1], listCreator);
		}
	}
	
	//REGISTER WITH USER
	public static class RegisterDeviceWithUser extends JSONRPCMethod {
		public RegisterDeviceWithUser(String userName, String registrationID) throws JSONException {
			super(OneListProtocol.RegisterDeviceWithUser.METHOD);
			
			this.methodParams.put(OneListProtocol.RegisterDeviceWithUser.PARAMETERS[0], registrationID);
			this.methodParams.put(OneListProtocol.RegisterDeviceWithUser.PARAMETERS[1], userName);
		}
	}
	
	/*
	 * Dont need to worry about this stuff. Place all subclasses above.
	 */
	private JSONRPCMethod(String method) throws JSONException {
		this.jsonMethod = new JSONObject();
		this.methodParams = new JSONObject();
		this.method = method;
		
		this.jsonMethod.put(METHOD_KEY, method);
		this.jsonMethod.put(PARAMS_KEY, this.methodParams);
	}
	
	public String getMethodName() {
		return this.method;
	}
	
	public JSONObject toJSONObject() {
		return this.jsonMethod;
	}
	
	private String method;
	protected JSONObject methodParams;
	protected JSONObject jsonMethod;
	
	public static final String METHOD_KEY = "method";
	public static final String PARAMS_KEY = "params";
}

/**
 * JSONRPCMethod format: to help visualize.
 * {
 * 	"method" : METHOD_NAME,
 *  "params" : {
 *  			PARAM_KEY_1 : "value1",
 *  			PARAM_KEY_2 : "value2",
 * 				PARAM_KEY_3 : "value3"
 * 				}
 * }
 */
